package br.com.arosa.dao;

import java.util.*;
import java.sql.*;
import br.com.easynet.database.DataSet;
import br.com.jdragon.dao.*;
import br.com.arosa.transfer.*;

public class Tb_clienteDAO extends ObjectDAO { 
	 public Tb_clienteDAO(DAOFactory dao) throws Exception {
		 setDAOFactory(dao);
 		 con = dao.create();
 	 }

	 public void insert(Tb_clienteT tb_clienteT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "insert into tb_cliente  ( id_cliente, id_cidade, nome, cpf, rg, endereco, numero, bairro, complemento, email, telefone) values ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_clienteT.getId_cliente());
			 pStmt.setObject(2, tb_clienteT.getId_cidade());
			 pStmt.setObject(3, tb_clienteT.getNome());
			 pStmt.setObject(4, tb_clienteT.getCpf());
			 pStmt.setObject(5, tb_clienteT.getRg());
			 pStmt.setObject(6, tb_clienteT.getEndereco());
			 pStmt.setObject(7, tb_clienteT.getNumero());
			 pStmt.setObject(8, tb_clienteT.getBairro());
			 pStmt.setObject(9, tb_clienteT.getComplemento());
			 pStmt.setObject(10, tb_clienteT.getEmail());
			 pStmt.setObject(11, tb_clienteT.getTelefone());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void update(Tb_clienteT tb_clienteT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "update tb_cliente set  id_cidade=?, nome=?, cpf=?, rg=?, endereco=?, numero=?, bairro=?, complemento=?, email=?, telefone=?  where  id_cliente=?";
 			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_clienteT.getId_cidade());
			 pStmt.setObject(2, tb_clienteT.getNome());
			 pStmt.setObject(3, tb_clienteT.getCpf());
			 pStmt.setObject(4, tb_clienteT.getRg());
			 pStmt.setObject(5, tb_clienteT.getEndereco());
			 pStmt.setObject(6, tb_clienteT.getNumero());
			 pStmt.setObject(7, tb_clienteT.getBairro());
			 pStmt.setObject(8, tb_clienteT.getComplemento());
			 pStmt.setObject(9, tb_clienteT.getEmail());
			 pStmt.setObject(10, tb_clienteT.getTelefone());
			 pStmt.setObject(11, tb_clienteT.getId_cliente());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void delete(Tb_clienteT tb_clienteT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "delete from tb_cliente where  id_cliente=?";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_clienteT.getId_cliente());
			 pStmt.execute();
		 } catch (Exception e)  {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 private List<Tb_clienteT> resultSetToObjectTransfer(ResultSet rs) throws Exception { 
		 List<Tb_clienteT> objs = new Vector();
		 while (rs.next()) { 
 			 Tb_clienteT tb_clienteT = new Tb_clienteT();
			 tb_clienteT.setId_cliente(rs.getInt("id_cliente"));
			 tb_clienteT.setId_cidade(rs.getInt("id_cidade"));
			 tb_clienteT.setNome(rs.getString("nome"));
			 tb_clienteT.setCpf(rs.getString("cpf"));
			 tb_clienteT.setRg(rs.getString("rg"));
			 tb_clienteT.setEndereco(rs.getString("endereco"));
			 tb_clienteT.setNumero(rs.getString("numero"));
			 tb_clienteT.setBairro(rs.getString("bairro"));
			 tb_clienteT.setComplemento(rs.getString("complemento"));
			 tb_clienteT.setEmail(rs.getString("email"));
			 tb_clienteT.setTelefone(rs.getString("telefone"));
			 objs.add(tb_clienteT);
 		 }
 		 return objs; 
 	 }
	 public List<Tb_clienteT> getAll() throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente"; 
			 pStmt = con.prepareStatement(sql);
			 rs = pStmt.executeQuery();
 			 List<Tb_clienteT> list = resultSetToObjectTransfer(rs);
 			 return list; 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getById(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  id_cliente=?"; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_clienteT.getId_cliente());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getById_cliente(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  id_cliente = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_clienteT.getId_cliente());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getById_cidade(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  id_cidade = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_clienteT.getId_cidade());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByNome(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(nome) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getNome()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByCpf(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(cpf) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getCpf()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByRg(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(rg) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getRg()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByEndereco(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(endereco) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getEndereco()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByNumero(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(numero) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getNumero()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByBairro(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(bairro) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getBairro()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByComplemento(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(complemento) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getComplemento()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByEmail(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(email) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getEmail()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_clienteT> getByTelefone(Tb_clienteT tb_clienteT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_cliente where  Upper(telefone) like Upper(?) "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, '%' + tb_clienteT.getTelefone()+'%' );
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 

 }